package testExport;

import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.ExportExcel;
import testEntity.LocalMemberExcle;

import com.cz073586.tools.util.Tools;
import com.local.model.LocalMember;
import com.local.page.LocalMemberPage;
import com.local.service.face.ParkwellMemberService;
import com.qianhai.common.spring.utils.SpringContextUtil;
import com.qianhai.common.utils.BeanUtil;

public class ExcelServlet extends HttpServlet
{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		doPost(req, resp);
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		// 取得文件名。
		String filename = "会员卡信息表";
		// 清空response
		resp.reset();
		// 设置response的Header
		resp.addHeader("Content-Disposition", "attachment;filename="
				+ new String(filename.getBytes("gb2312"), "ISO8859-1" )+".xls");
		resp.setContentType("application/vnd.ms-excel;charset=gb2312");
		
		// 初始化数据
		ParkwellMemberService parkwellMemberService = SpringContextUtil.getApplicationContext().getBean(ParkwellMemberService.class);
		
		LocalMemberPage<LocalMember> pageInfo=new LocalMemberPage<LocalMember>();
		pageInfo.setPageSize(99999);
		pageInfo.setMemberName(!Tools.isEmpty(req.getParameter("name"))?req.getParameter("name"):null);
		pageInfo.setCarNo(!Tools.isEmpty(req.getParameter("carNo"))?req.getParameter("carNo"):null);
		pageInfo.setStatus(!Tools.isEmpty(req.getParameter("status"))?Integer.parseInt(req.getParameter("status")):0);
		SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(!Tools.isEmpty(req.getParameter("startTime"))){
			try
			{
				pageInfo.setStartTime(dd.parse(req.getParameter("startTime")));
			} catch (ParseException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(!Tools.isEmpty(req.getParameter("endTime"))){
			try
			{
				pageInfo.setEndTime(dd.parse(req.getParameter("endTime")));
			} catch (ParseException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List<LocalMember> list=parkwellMemberService.findAllParkwellMemberByQueryForm(pageInfo);
		
		ExportExcel<LocalMemberExcle> ex = new ExportExcel<LocalMemberExcle>();
		String[] headers = { "车主", "车牌号", "会员类型","车辆类型","是否过期", "联系方式", "地址","操作时间","开始时间", "结束时间"};
		
		List<LocalMemberExcle> dataset = new ArrayList<LocalMemberExcle>();
		for (LocalMember lm : list)
		{
			LocalMemberExcle le=new LocalMemberExcle();
			BeanUtil.copyProperties(le, lm);
			dataset.add(le);
		}
		OutputStream out=resp.getOutputStream();
		ex.exportExcel(filename,headers, dataset, out);
		out.close();  
	}
}